package com.cy.dda.web.admin.collection;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cy.dda.comm.utils.CommonUtils;
import com.cy.dda.comm.utils.ExportExcelUtils;
import com.cy.dda.model.PageBean;
import com.cy.dda.model.Result;
import com.cy.dda.model.ao.collection.CollectionRecordAddAO;
import com.cy.dda.model.ao.collection.CollectionRecordEditAO;
import com.cy.dda.model.query.collection.CollectionRecordListAdminQuery;
import com.cy.dda.model.vo.collection.CollectionRecordDetailInfoAdminVO;
import com.cy.dda.model.vo.collection.CollectionRecordListAdminVO;
import com.cy.dda.service.collection.CollectionRecordService;
import com.cy.dda.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 后台-催收记录管理
 *
 * @author: tanyilin
 * @create: 2018年11月6日 下午4:32:04
 */
@Slf4j
@RestController
@RequestMapping("/admin/collection/record")
@Api(tags = "后台-催收记录管理")
public class AdminCollectionRecordController extends BaseController {

	@Autowired
	private CollectionRecordService collectionRecordService;
	
	@ApiOperation("催收记录列表")
	@GetMapping("/list")
	public PageBean<CollectionRecordListAdminVO> selectCollectionRecordListAdmin(CollectionRecordListAdminQuery query) {
		
		PageBean<CollectionRecordListAdminVO> pageBean = collectionRecordService.selectCollectionRecordListAdmin(query);
		
		return pageBean;
	}
	
	@ApiOperation("催收记录详情信息")
	@GetMapping("/get/detail/info")
	public Result<CollectionRecordDetailInfoAdminVO> getDetailInfo(@RequestParam Long collectionRecordId) {
		CollectionRecordDetailInfoAdminVO modelVo = collectionRecordService.selectCollectionRecordDetailInfoAdminVO(collectionRecordId);
		return ok("查询成功", modelVo);
	}
	
	@ApiOperation("导出列表")
	@GetMapping("/list/export")
	public void listExport(CollectionRecordListAdminQuery query, HttpServletRequest req, HttpServletResponse resp) {
		query.setPage(1);
		query.setLimit(5000);
		List<CollectionRecordListAdminVO> data = this.selectCollectionRecordListAdmin(query).getData();
		String title = "催收记录列表";
		String[] headers = {"委案机构", "委案类型", "委案状态", "客户", "工作城市", "催收类型", "回款情况", 
				"回款金额", "逾期金额", "逾期期数", "催收员", "催记时间", "催收备注"};
		String[] fieldNames = {"orgName", "casesTypeText", "casesStatusText", "customerName", "city", "collectionRecordTypeText", "repayTypeText", 
				"amount", "overdueAmount", "overduePeriod", "collectionUserName", "createTime",  "collectionRemark"};
		ExportExcelUtils<CollectionRecordListAdminVO> ex = new ExportExcelUtils<>();
		try(OutputStream out = resp.getOutputStream()) {
			req.setCharacterEncoding("UTF-8");
			resp.setCharacterEncoding("UTF-8");
			resp.setContentType("application/x-download");
			String fileName = CommonUtils.getCurrentTimeAndRandomNumber().concat(".xls");
			fileName = new String(fileName.getBytes("gb2312"), "ISO8859-1");
			resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			ex.exportExcel(title, headers, fieldNames, data, out);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@ApiOperation("新增催记")
	@PostMapping("/do/add")
	public Result doAdd(@Validated @RequestBody CollectionRecordAddAO addAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		collectionRecordService.addCollectionRecord(addAO, null);
		return ok();
	}
	
	@ApiOperation("催记编辑")
	@PostMapping("/do/edit")
	public Result doEdit(@Validated @RequestBody CollectionRecordEditAO editAO, BindingResult bindingResult) {
		this.checkError(bindingResult);
		state(!CommonUtils.isEmpty(editAO.getCollectionRecordId()), "催收记录ID不可为空");
		collectionRecordService.doEdit(editAO);
		return ok();
	}
	
}
